package com.vegetable.modules.controller.sys;

import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.sys.SysUserEntity;
import com.vegetable.modules.form.SysLoginForm;
import com.vegetable.modules.service.sys.SysCaptchaService;
import com.vegetable.modules.service.sys.SysUserService;
import com.vegetable.modules.service.sys.SysUserTokenService;
import com.vegetable.modules.service.sys.OnlineUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 登录相关
 *
 * @author Mark sunlightcs@gmail.com
 */

@RestController
@AllArgsConstructor
@Api(value = "登录相关", tags = "登录相关")
public class SysLoginController extends AbstractController {
	private SysUserService sysUserService;
	private SysUserTokenService sysUserTokenService;
	private SysCaptchaService sysCaptchaService;
	private OnlineUserService onlineUserService;

	/**
	 * 验证码
	 */
/*	@GetMapping("captcha.jpg")
	@ApiOperation(value = "验证码", notes = "验证码")
	public void captcha(HttpServletResponse response, String uuid)throws IOException {
		response.setHeader("Cache-Control", "no-store, no-cache");
		response.setContentType("image/jpeg");

		//获取图片验证码
		BufferedImage image = sysCaptchaService.getCaptcha(uuid);

		ServletOutputStream out = response.getOutputStream();
		ImageIO.write(image, "jpg", out);
		IOUtils.closeQuietly(out);
	}*/

	/**
	 * 登录
	 */
	@PostMapping("/sys/login")
	@ApiOperation(value = "登录", notes = "登录")
	public R login(@RequestBody SysLoginForm form)throws IOException {
		/*boolean captcha = sysCaptchaService.validate(form.getUuid(), form.getCaptcha());
		if(!captcha){
			return R.fail("验证码不正确");
		}*/
		//用户信息
		SysUserEntity user = sysUserService.queryByUserName(form.getUsername());
		//账号不存在、密码错误
		if(user == null || !user.getPassword().equals(form.getPassword())) {
			return R.fail("账号或密码不正确");
		}
		//账号锁定
		if(user.getStatus() == 0){
			return R.fail("账号已被锁定,请联系管理员");
		}

		//生成token，并保存到数据库
		R tokenResult = sysUserTokenService.createToken(user.getUserId());
		
		// 添加用户到在线用户列表
		if (tokenResult.isSuccess()) {
			String token = (String) tokenResult.getData();
			onlineUserService.addUser(user, token);
		}
		
		return tokenResult;
	}


	/**
	 * 退出
	 */
	@PostMapping("/sys/logout")
	@ApiOperation(value = "退出", notes = "退出")
	public R logout() {
		// 从在线用户列表中移除
		onlineUserService.removeUser(getUserId());
		
		sysUserTokenService.logout(getUserId());
		return R.status(true);
	}

	/**
	 * 获取Token用于NCC系统调用保存银行档案接口
	 */
	/*@ApiOperation(value = "退出", notes = "退出")
	@GetMapping("/sys/getToken")
	public R getToken() {
		//用户信息
		SysUserEntity user = sysUserService.queryByUserName("admingettoken");
		//生成token，并保存到数据库
		return sysUserTokenService.createToken(user.getUserId());
	}*/


}